package com.kj.report.service.impl;


import com.kj.report.model.PackageInfo;
import com.kj.report.model.PayOrder;
import com.kj.report.model.ReportView;
import com.kj.report.mongoRepository.ReportViewRepository;
import com.kj.report.service.PayOrderService;
import com.kj.report.service.ReportOrderService;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

@Service
public class ReportOrderServiceImpl implements ReportOrderService{
    Logger logger = Logger.getLogger(ReportV2ServiceImpl.class);
    @Resource
    private PayOrderService payOrderServiceImpl;

    @Resource
    private PackageInfoServiceImpl packageInfoServiceImpl;
    @Resource
    private ReportViewRepository reportViewRepository;
    @Override
    public PayOrder inserPayOrder(String reportId, Integer deviceId, Long customerId) {
        PayOrder byReportId = payOrderServiceImpl.findPayOrderByReportId(reportId);
        if (byReportId != null) {
            //throw  new IllegalArgumentException("订单已生成");
            return byReportId;

        }

        try {
            PayOrder entity = new PayOrder();
            entity.setUserId(customerId);
            entity.setOrderNum(byReportId.getOrderNum());
            entity.setOrderTime(new Date());
            entity.setPayStatus(1);

            entity.setDeviceId(deviceId);
            PackageInfo packageInfo = packageInfoServiceImpl.findPackageByDeiveId(deviceId);
            if (packageInfo != null) {
                entity.setCommodityName(packageInfo.getName());
                //entity.setPayPrice(packageInfo.getPrice());
                entity.setOrderPrice(packageInfo.getPrice());
                entity.setPackageId(packageInfo.getPackageId());
            }

            ReportView reportView = reportViewRepository.findById(reportId);
            if (reportView != null) {
                entity.setMacAddress(reportView.getDeviceMacAddress());
                entity.setSnNum(reportView.getDeviceSnNum());
                entity.setAgentId(reportView.getAgentId());
                entity.setAgentIds(reportView.getAgentIds());
            }

            entity.setInspectId(reportId);
            payOrderServiceImpl.insert(entity);
            return entity;
        } catch (Exception e) {
            logger.info(reportId+"解析报告时生成订单错误信息"+e.getMessage());
        }
        return null;
    }
}
